package com.cqfae.deploycentral.app.utils;

import com.cqfae.deploycentral.app.job.JiraJob;
import com.cqfae.deploycentral.app.web.vo.FileUploadVo;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Component
public class DCFileUtil {

    private static Logger logger = LoggerFactory.getLogger(JiraJob.class);

    private DCFileUtil() {
        super();
    }

    /**
     * 上传文件
     *
     * @param file
     * @param CachePath
     * @return
     */
    public static FileUploadVo fileUpload(MultipartFile file, String CachePath) {
        // 获得上传文件名字
        String originalFilename = file.getOriginalFilename();
        // 去掉空格文件名中的空格，避免因系统对空格不同识别符导致的错误
        originalFilename = originalFilename.replace(" ", "");
        // 获取上传文件类型
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf('.') + 1, originalFilename.length());

        // 上传到对应地址
        String fileRealPath = CachePath + File.separator + originalFilename;
        File destFile = new File(fileRealPath);
        logger.info("file copy to {}", fileRealPath);
        try {
            FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
            logger.info("file copy done");
        } catch (IOException e) {
            logger.error("file copy IOException", e);
            Assert.isTrue(false, "文件上传异常，请重试");
        }
        // 组装返回信息
        FileUploadVo fileUploadVo = new FileUploadVo();
        fileUploadVo.setFileId(originalFilename);
        fileUploadVo.setFileName(originalFilename);
        fileUploadVo.setFileType(String.valueOf(fileSuffix));
        fileUploadVo.setFilePath(File.separator + originalFilename);

        return fileUploadVo;
    }


    /**
     * 删除文件
     *
     * @param fileName
     * @return
     */
    public static Integer fileDelete(String fileName, String CachePath) {
        String filePath = CachePath + File.separator + fileName;
        File file = new File(filePath);
        boolean deleteFlag = false;
        if (file.exists()) {
            deleteFlag = file.delete();
        }
        if (deleteFlag) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 文件移动
     *
     * @param fileFromPath
     * @param fileToPath
     */
    public static boolean fileMove(String fileFromPath, String fileToPath) {
        File fileFrom = new File(fileFromPath);
        File fileTo = new File(fileToPath);
        // 源地址和目标地址相同时不作操作
        if (!fileFrom.exists()) {
            logger.info("file is not exist");
            return false;
        }
        if (fileFrom.equals(fileTo)) {
            return true;
        }
        try {
            logger.info("file copy：\n\t\tfrom：{}\n\t\tto：{}", fileFromPath, fileToPath);
            FileUtils.copyFile(fileFrom, fileTo);
            logger.info("file copy done");
            // 删除源文件
            boolean result = fileFrom.delete();
            logger.info("delete source file {}", result);
        } catch (IOException e) {
            logger.error("FileSyncServiceImpl add IOException : {}", e);
            return false;
        }
        return true;
    }


}
